<?php
class Admin_AclresourceController extends Custom_Controller_Action
{
    public function init()
    {
        
        
        
    }
    public function indexAction()
    {
        $aclresource = new User_Model_DbTable_AclResource();
        $page = $this->getRequest()->getParam('page');
      
        $aclresource->setPaginator($this->Config->params->recordsPerPage, $page);
     
        //$this->setSort($aclresource);
        $this->view->resources = $aclresource->getAll();
        $this->view->paginator = $aclresource->getPaginator();
        $this->view->page = $page;
        
    }
    public function createAction()
    {
          $form = new Admin_Form_CreateAclResource();
          $this->view->createresourceform = $form;
          if($this->getRequest()->isPost())
          {
              $formdata = $this->getRequest()->getPost();
              if($form->isValid($formdata))
              {
                  
                  $aclresource = new User_Model_DbTable_AclResource();
                  $data = $form->getValidValues($formdata);
                  $res = $aclresource->insert($data);
                  if($res)
                  {
                      
                      $this->addMessage('Resource is added successfully');
                      
                  }    
                  else $this->addMessage('Sorry resouce is not added');
                  
                  $this->RedirectUrl('/admin/aclresource');
              }        
          }        
        
        
    }
    public function editAction()
    {
        $resourceid = $this->getRequest()->getParam('id');
        $page = $this->getRequest()->getParam('page');
        $aclresource = new User_Model_DbTable_AclResource();
        $resource = $aclresource->getOne($resourceid);
        $form = new Admin_Form_CreateAclResource($resourceid);
        $this->view->form = $form;
        $form->populate($resource);
        if($this->getRequest()->isPost())
        {        
            $formdata = $this->getRequest()->getPost();
            $data = $form->getValidValues($formdata);
            $where = 'id='.$resourceid;
            $res = $aclresource->update($data, $where);
            if($res)
            {
                $this->addMessage('Resource is edited successfully');
            }
            else
            {
                $this->addMessage('Resource is not edited','error');
            }    
            if($page)$link = '/page/'.$page;
             $this->RedirectUrl('/admin/aclresource/index'.$link);
        }
    }
    
    public function deleteAction()
    {
        $resourceid = $this->getRequest()->getParam('id');
        $page = $this->getRequest()->getParam('page');
        $aclresource = new User_Model_DbTable_AclResource();
        $where ='id='.$resourceid;
        $res = $aclresource->delete($where);
        if($res)
        {
            $this->addMessage("Resource is deleted successfully");
            
        } 
        else
        {
            $this->addMessage("Resource is not deleted",'error');
        }
         if($page)$link = '/page/'.$page;
             $this->RedirectUrl('/admin/aclresource/index'.$link);
    }
    
     public function autogenerateAction()
    {
        $objreflecter = new Custom_Action_Helper_AssetsList();
        $resources = $objreflecter->getList();
        $objresource = new User_Model_DbTable_AclResource();
      foreach($resources as $key=>$res)
      {
          foreach($res as $key1=>$mod )
          {
              if($key!='custom')
              {     
                 $resource = $key.'_'.$key1;
                 $module =$key;
              $val = $objresource->getByColumns(array('resource_module'=>$module,'resource'=>$resource));
              if(count($val)==0)
              {
               
                  $data['resource'] = $resource;
                  $data['resource_module'] = $module;
                  $data['created_date']= date('Y-m-d H:i:s');
                  $result = $objresource->insert($data);
              }    
             }
              
          }  
      }
      if($result)
      {
          $this->addMessage('Autogenerate is successfull');
          
      }
      else
      {
          $this->addMessage("There is nothing to generate");
      }    
      
      $this->RedirectUrl('/admin/aclresource');
        
    }
}
?>
